<?php

namespace chain\Model;
use  chain\Db\Db;

class Model{

    protected $db = null;
    protected $opt = array();
    //初始化子类Model时 给出table和pri
    protected $table;
    protected $pri;


    public function __construct()
    {
        $this->db = Db::getInstance(require ROOT_PATH . "/config/DbConfig.php");
        $this->setTable();
        $this->getPri();
        $this->InitOpt();
    }

    /**
     * 类名全小写成复数形式 为 对于表名
     */
    public function setTable(){
        echo get_called_class() . "<br>";
        $tmp = explode("\\",get_called_class());
        $model = end($tmp);
        $this->table = strtolower(substr($model,0,-5)) . "s";
    }


    public function getPri(){
        $sql = "desc " . $this->table;
        $res = $this->db->queryString($sql);
        foreach($res as $key=>$value){
            if($value['Key'] == "PRI"){
                $this->pri = $value['Field'];
            }
        }

    }

    public function InitOpt(){
        $this->opt = array('table' => $this->table , 'pri' => $this->pri , 'where' => '' ,
                            'orderby' => '' , 'having' => '' , 'group' => '' ,
                            'limit' => '' , 'fields' => '*',"");

    }

    /**
     * @param $name
     * @param $arguments
     * @return $this
     */
    public function __call($name, $arguments)
    {
        if(in_array($name,array("where","fields","limit","orderby"))){
            switch ($name){
                case "fields":
                    if(empty($arguments)){
                        $this->opt['fields'] = "*";
                    }else if(count($arguments) == 1){
                        $this->opt['fields'] = $arguments[0];
                    }else{
                        $this->opt['fields'] = $arguments;
                    }
                    return $this;
                case "where":
                    if(!empty($arguments)){
                        $this->opt['where'] = $arguments;
                    }
                    return $this;
                case "limit":
                    if(empty($arguments)){
                        $this->opt['limit'] = "limit 0,1";
                        return $this;
                    }
                    if(!isset($arguments[1])){
                        $this->opt['limit'] = "limit {$arguments[0]},1";
                        return $this;
                    }
                    $this->opt['limit'] = "limit {$arguments[0]},{$arguments[1]}";
                    return $this;
                case "orderby":
                    //默认id 和 desc
                    if(empty($arguments)){
                        $this->opt['orderby'] = "order by {$this->pri} desc";
                        return $this;
                    }
                    if(!isset($arguments[1])){
                        $this->opt['orderby'] = "order by {$arguments[0]} desc";
                        return $this;
                    }

                    $this->opt['orderby'] = "order by {$arguments[0]} {$arguments[1]}";
                    return $this;

            }

        }else{
            exit("Unsupported method");
        }
    }


    /**
     *
     */
    public function find(){
        //select FIELD FORM TABLE WHERE LIMIT
        $this->opt['limit'] = "limit 0,1";
        $this->get();
    }

    /**
     * @return array
     */
    public function get(){
        //select FIELD FORM TABLE WHERE LIMIT
        $opt = array_filter($this->opt);
        if(is_array($this->opt['fields'])){
            $tmp_field = implode(",",$this->opt['fields']);
        }else{
            $tmp_field = $this->opt['fields'];
        }

        $sql = "select {$tmp_field} from {$this->table} ";

        if(!empty($this->opt['where'])){
            $tmp_where = $this->db->getWhereStatement($this->opt['where']);
            $sql .= $tmp_where;
        }
        $sql .= " " . $this->opt['limit'];
        return $this->db->queryString($sql);
    }

    public function all(){
        return $this->get();
    }

}


?>